package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import java.util.List;
import java.util.Map;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNoticeTemplateParameterVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNoticeTemplateRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowRuTimeoutSettingRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.FlowCommonCodeConstants;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNoticeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 模板管理
 *
 * @author zhangnn
 * @date 2020-11-19
 */
@RestController
@RequestMapping("/notice/template")
public class FlowNoticeTemplateController {

    @Autowired
    private FlowNoticeTemplateRepository flowNoticeTemplateRepository;

    @Autowired
    private FlowRuTimeoutSettingRepository flowRuTimeoutSettingRepository;

    /**
     * 查询通知模板列表
     *
     * @param flowNoticeTemplateQueryVo
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FlowNoticeTemplateQueryVo flowNoticeTemplateQueryVo) {
        ResponseResult responseResult = flowNoticeTemplateRepository.list(flowNoticeTemplateQueryVo);
        return responseResult;
    }

    /**
     * 新增模板
     *
     * @param flowNoticeTemplateAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated FlowNoticeTemplateAddVo flowNoticeTemplateAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        if (flowNoticeTemplateAddVo.getStatus()){
            String type = flowNoticeTemplateAddVo.getType();
            if (flowNoticeTemplateRepository.isTemplateExist(type)) {
                return ResponseResult.fail(ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errCode(), ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errMessage());
            }
        }
        return ResponseResult.success(flowNoticeTemplateRepository.insert(flowNoticeTemplateAddVo));
    }

    /**
     * 更新模板
     */
    @PostMapping("/edit")
    public ResponseResult update(@RequestBody @Validated FlowNoticeTemplateUpdateVo flowNoticeTemplateUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        if (flowNoticeTemplateUpdateVo.getStatus()){
            String type = flowNoticeTemplateUpdateVo.getType();
            Long id = flowNoticeTemplateUpdateVo.getId();
            if (flowNoticeTemplateRepository.isTemplateExist(type,id)) {
                return ResponseResult.fail(ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errCode(), ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errMessage());
            }
        }
        Integer updateRow = flowNoticeTemplateRepository.update(flowNoticeTemplateUpdateVo);
        if (updateRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.UPDATE_FAILED.getCode(), FlowCommonCodeConstants.UPDATE_FAILED.getDesc());
    }

    /**
     * 删除模板
     */
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody @Validated FlowNoticeTemplateDeleteVo flowNoticeTemplateDeleteVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        //是否需要验证该模板被引用的情况
        //todo
        Integer deleteRow = flowNoticeTemplateRepository.delete(flowNoticeTemplateDeleteVo);
        if (deleteRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.DELETE_FAILED.getCode(), FlowCommonCodeConstants.DELETE_FAILED.getDesc());
    }

    /**
     * 模板详情
     */
    @PostMapping("/detail")
    public ResponseResult detail(@RequestBody @Validated FlowNoticeTemplateDetailVo flowNoticeTemplateDetailVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        ResponseResult detailResult = flowNoticeTemplateRepository.detail(flowNoticeTemplateDetailVo);
        return detailResult;
    }

    /**
     * 状态启用
     */
    @PostMapping("/startStatus")
    public ResponseResult startStatus(@RequestBody @Validated FlowNoticeTemplateStatusVo flowNoticeTemplateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        if (flowNoticeTemplateStatusVo.getStatus()){
            String type = flowNoticeTemplateStatusVo.getType();
            if (flowNoticeTemplateRepository.isTemplateExist(type)) {
                return ResponseResult.fail(ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errCode(), ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errMessage());
            }
        }
        Integer deleteRow = flowNoticeTemplateRepository.startStatus(flowNoticeTemplateStatusVo);
        if (deleteRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.START_FAILED.getCode(), FlowCommonCodeConstants.START_FAILED.getDesc());
    }

    /**
     * 根据模板类型查询模板列表
     *
     * @param type
     * @return
     */
    @GetMapping("/getTemplateListByType")
    public ResponseResult getTemplateListByType(@RequestParam(value="type") String type) {
        List<FlowNoticeTemplateVo> flowNoticeTemplates = flowNoticeTemplateRepository.getTemplateListByType(type);
        return ResponseResult.success(flowNoticeTemplates);
    }

    /**
     * 获取模板参数列表
     *
     * @return
     */
    @GetMapping("/listNoticeTemplateParameter")
    public ResponseResult listNoticeTemplateParameter(@RequestParam(value="type") String type) {
        List<FlowNoticeTemplateParameterVo> flowNoticeTemplateParameters = flowNoticeTemplateRepository.listNoticeTemplateParameter(type);
        return ResponseResult.success(flowNoticeTemplateParameters)
                .setTotalCount(flowNoticeTemplateParameters.size())
                .setPageSize(flowNoticeTemplateParameters.size());
    }

    /**
     * 获取模板类型列表
     *
     * @return
     */
    @GetMapping("/listNoticeTemplateType")
    public ResponseResult listNoticeTemplateType() {
        List<Map<String, String>> noticeTemplateType = flowNoticeTemplateRepository.listNoticeTemplateType();
        return ResponseResult.success(noticeTemplateType)
                .setTotalCount(noticeTemplateType.size())
                .setPageSize(noticeTemplateType.size());
    }

    @PostMapping("/reSendNoticeMessage")
    public ResponseResult reSendNoticeMessage(@RequestBody Map params) {
        Object ruNoticeChannelId = params.get("ruNoticeChannelId");
        if (ruNoticeChannelId != null) {
            String status = flowNoticeTemplateRepository.reSendNoticeMessage(ruNoticeChannelId.toString());
            return ResponseResult.success(status);
        } else {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), ErrorCode.PARAMETER_ERROR.errMessage());
        }
    }



}
